package com.eastcom.providor.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_MetalKpiExpand;
import com.eastcom.providor.dao.IMetalKpiExpandDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IMetalKpiExpandService;

public class MetalKpiExpandService extends BaseService implements IMetalKpiExpandService {

	private IMetalKpiExpandDao metalKpiExpandDao;

	public IMetalKpiExpandDao getMetalKpiExpandDao() {
		return metalKpiExpandDao;
	}

	public void setMetalKpiExpandDao(IMetalKpiExpandDao metalKpiExpandDao) {
		this.metalKpiExpandDao = metalKpiExpandDao;
	}

	public boolean editMetalKpiBaseByKpiId(List<Atom_MetalKpiExpand> metalKpiExpands, String kpiId) {
        
		return false;
	}

	public boolean editLstVersionMetalKpiExpandByKpiId(String kpiId, Atom_MetalKpiExpand metalKpiExpand) {
		if(kpiId==null||metalKpiExpand==null)
			return true;
		
		try{
			Map<Object,Map> result=metalKpiExpandDao.selectKpiLastVersionByKpiId(kpiId);
			Map versionMap=result==null?null:result.get(kpiId);
			Integer version=versionMap==null||versionMap.get("version")==null?0:Integer.parseInt((String)versionMap.get("version"))+1;
			metalKpiExpand.setVersion(version.toString());
			metalKpiExpand.setUpdateTime(new Date());
			metalKpiExpandDao.editLstVersionMetalKpiExpandByKpiId(kpiId, metalKpiExpand);
			return true;
		}
		catch(Exception e){
			log.error("MetalKpiExpandService editLstVersionMetalKpiExpandByKpiId error!",e);
			e.printStackTrace();
		}
		
		return false;
	}

	public boolean saveMetalKpiBase(List<Atom_MetalKpiExpand> metalKpiExpands) {
		try{
			metalKpiExpandDao.saveMetalKpiExpands(metalKpiExpands);
			return true;
		}
		catch(Exception e){
			log.error("MetalKpiExpandService saveMetalKpiBase error!",e);
			e.printStackTrace();
		}
		return false;
	}

	public List<Atom_MetalKpiExpand> selectMetalKpiExpandByKpiId(String kpiId,boolean initialKpiBase) {
        try{
        	return metalKpiExpandDao.selectMetalKpiExpandByKpiId(kpiId, initialKpiBase);
		}
		catch(Exception e){
			log.error("MetalKpiExpandService selectMetalKpiBaseByKpiId with "+kpiId+" error!",e);
			e.printStackTrace();
		}
		
		return null;
	}

	public Map<Object, Map> selectKpiLastVersion() {
		try{
        	return metalKpiExpandDao.selectKpiLastVersion();
		}
		catch(Exception e){
			log.error("MetalKpiExpandService selectKpiLastVersion error!",e);
			e.printStackTrace();
		}
		
		return null;
	}

	public Map<Object,Map> selectMetalKpiExpOfLastVersion() {
		try{
        	return metalKpiExpandDao.selectMetalKpiExpOfLastVersion();
		}
		catch(Exception e){
			log.error("MetalKpiExpandService selectMetalKpiExpOfLastVersion error!",e);
			e.printStackTrace();
		}
		
		return null;
	}

}
